package utils;

import java.io.File;
import java.util.List;

import javax.xml.soap.Node;
import javax.xml.transform.stream.StreamSource;

import net.sf.saxon.Configuration;
import net.sf.saxon.om.Axis;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.tree.iter.AxisIterator;

public class XQuery {
	
	
	public static void main(String[] args)
	{
		getResult();
	}
	
	static String file = "C:\\DATA\\SVN\\ProjetXML\\BackOffice\\files\\PPAScript1.xml";
	
	@SuppressWarnings("deprecation")
	public static void getResult()
	{
		try
		{
			Configuration config = new Configuration();
			StaticQueryContext staticContext = config.newStaticQueryContext();
			XQueryExpression exp = staticContext.compileQuery("//etape");
			DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
			dynamicContext.setContextNode(config.buildDocument(new StreamSource(new File(file))));
			//SequenceIterator<NodeInfo> books = exp.iterator(dynamicContext);
			List<NodeInfo> list = exp.evaluate(dynamicContext);
			
			for(int i = 0 ;i < list.size();i++)
			{
				showNode(list.get(i));
			}
//			while (true) 
//			{
//			    NodeInfo etape = books.next();
//			    if (etape==null) break;
//			    etape.getStringValue();
//			    System.out.println(etape.getStringValue());
//			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	static void showNode(NodeInfo node)
	{
		NodeInfo _node;
		try
		{
			System.out.println(node.getDisplayName());
			if(node.hasChildNodes())
			{
				while((_node = node.iterateAxis(Axis.CHILD).next()) != null)
				{
					System.out.println(_node.getStringValue());
				}
			}
		}
		catch(Exception e)
		{
			
		}
				
	}
	
	
	
}
